﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WFNetLib.GSMModem;
using System.Data.OleDb;

namespace 自消费
{
    public partial class TaskThread
    {
        CardData InitDevice()
        {
            string str;
            Com.ReadTimeout = 1000;
            int x;
            ATCommand atComm = new ATCommand(Com);
            CardData card = new CardData();
            try
            {
                if (!atComm.SendAT("ATE0"))
                {
                    OnAddLog("无卡", (int)TaskThreadEventType.Log);
                    return null;
                }
                if (!atComm.SendAT("AT+CSQ"))
                {
                    return null;
                }
                str = atComm.ATResult.Replace("\r\n", "");
                str = str.Substring(str.IndexOf("+CSQ: ") + "+CSQ: ".Length, str.IndexOf(',') - str.IndexOf("+CSQ: ") - "+CSQ: ".Length);
                if (Convert.ToInt32(str) < 99)
                {
                    //OnAddLog("信号良好", (int)TaskThreadEventType.Log);
                    OnAddLog(str, (int)TaskThreadEventType.CSQ);
                }
                else
                {
                    OnAddLog("无信号", (int)TaskThreadEventType.Log);
                    return null;
                }
                if (!atComm.SendAT("AT+CIMI"))
                {
                    OnAddLog("无SIM卡ID", (int)TaskThreadEventType.Log);
                    return null;
                }
                card.ID = atComm.ATResult.Replace("\r\n", "");
                switch (device.type)
                {
                    case 4://华为GTM900B
                        if (atComm.SendAT("AT%CID"))
                        {
                            card.iccid = atComm.ATResult.Replace("\r\n", "");
                        }
                        break;
                    case 3://WAVECOM MODEM MULTIBAND  900E  1800 
                        if (atComm.SendAT("AT+CCID"))
                        {
                            card.iccid = atComm.ATResult.Replace("\r\n", "");
                            x=card.iccid.IndexOf("+CCID: \"");
                            if(x==-1)
                                break;
                            x=x+"+CCID: \"".Length;
                            card.iccid = card.iccid.Substring(x, card.iccid.IndexOf("\"", x) - x);
                        }
                        break;
                    default:
                        if (atComm.SendAT("AT+CCID"))
                        {
                            card.iccid = atComm.ATResult.Replace("\r\n", "");
                        }
                        break;
                }
                              
                if (!atComm.SendAT("AT+CGSN"))
                {
                    OnAddLog("未知设备串码", (int)TaskThreadEventType.Log);
                    return null;
                }
                card.imei = atComm.ATResult.Replace("\r\n", "");

                atComm.SendAT("AT+CMGF=0");
                atComm.SendAT("AT+CLIP=1");
                atComm.SendAT("AT+CSCA?");
                atComm.SendAT("AT+CNMI=2,1,0,2");
                atComm.SendAT("AT+CPMS=\"SM\",\"SM\"");
                string sql;
                sql = "select * from card where id='" + card.ID + "'";
                OleDbDataReader reader = WFNetLib.ADO.AccessOP.ReaderExecuteSQL(sql);
                if (reader.Read())//存在
                {
                    reader.Close();
                    return card;
                }
                else//不存在，添加到数据库中
                {
                    reader.Close();
                    CardDataDBOption.Insert(card);
                    return card;
                }
            }
            catch (InvalidOperationException ex)//端口被关闭,任务结束
            {
                throw ex;
            }
            catch (System.Exception ex)
            {

            }
            finally
            {
               
            }
            return null;
        }
    }
}
